package com.projectframe.base.utils;

import java.lang.reflect.Field;

import org.apache.log4j.Logger;

import com.projectframe.base.exception.BaseException;

public class HibernateUtil
{
    private static Logger LOG = Logger.getLogger(HibernateUtil.class);

    public static final void checkDataVersion(final Object data1, final Object data2)
    {
        if (null != data1)
        {
            try
            {
                final Field data1Version = data1.getClass().getDeclaredField("version");
                data1Version.setAccessible(true);
                final long data1Verlong = data1Version.getLong(data1);

                if (null == data2)
                {
                    final BaseException pos3ge = new BaseException("B341005");
                    pos3ge.setStatus(1);
                    throw pos3ge;
                }

                final Field data2Version = data2.getClass().getDeclaredField("version");

                data2Version.setAccessible(true);
                final long data2Verlong = data2Version.getLong(data2);

                if (data1Verlong != data2Verlong)
                {
                    final BaseException pos3ge = new BaseException("B341005");
                    pos3ge.setStatus(1);
                    throw pos3ge;
                }
            }
            catch (NoSuchFieldException e)
            {
                LOG.error("check version error, " + e.getMessage(), e);
            }
            catch (IllegalArgumentException e)
            {
                LOG.error("check version error, " + e.getMessage(), e);
            }
            catch (IllegalAccessException e)
            {
                LOG.error("check version error, " + e.getMessage(), e);
            }
        }
    }
}
